﻿using System;
using System.Threading;
using Microsoft.AspNetCore.SignalR.Client;
using Quantum.Core;

namespace Quantum.SignalR
{
    public class Requester : SignalRConnection, IRequestBehavior
    {
        public void Send(
            string message,
            Action<string> onSucessed,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            Connection.On("Send", onSucessed);
            Connection.InvokeAsync("Send", message, cancellationToken).GetAwaiter().GetResult();
        }

        public void Send<TIn, TOut>(
            Outgoing<TIn> message,
            Action<Incoming<TOut>> onSucessed,
            CancellationToken cancellationToken = new CancellationToken())
        {
            Connection.On("SendObjects", onSucessed);
            Connection.InvokeAsync("SendObjects", typeof(Incoming<TOut>), message, cancellationToken).GetAwaiter().GetResult();
        }
    }
}
